<?php

/**
 * MySQLList
 *
 * Given 2 columns in a MySQL table, create an associative array mapping one to
 * the other. This stems from the need to create lists of options for select
 * boxes 
 *
 * @package 
 * @author Michael Thomas 
 * @copyright 2008
 * @version 1.0
 * @access public
 */
class MySQLList
{
  private $table;
  private $keyColumn;
  private $valueColumn;
  private $constraint;
  private $database;

  public function __construct($table, $keyColumn, $valueColumn,
      $constraint = NULL, $database = NULL)
  {
    $this->table = $table;
    $this->keyColumn = $keyColumn;
    $this->valueColumn = $valueColumn;
    $this->constraint = $constraint;
    $this->database = $database;
  }

  public function getArray($data = array())
  {
    if (!is_null($this->database)) {
      $from = "$this->database.$this->table";
    } else {
      $from = $this->table;
    }
    $result = mysql_query("SELECT $this->keyColumn, $this->valueColumn
                           FROM $from
                           $this->constraint");
    while (list($key, $value) = mysql_fetch_array($result, MYSQL_NUM)) {
      $data[$key] = $value;
    }
    return $data;
  }
}

?>